import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutterapp/mobx/store/account_store.dart';
import 'package:provider/provider.dart';

class MobXPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('mobPage build');
    return Scaffold(
      appBar: AppBar(
        title: Text('mobx'),
      ),
      body: Provider<AccountStore>(
        create: (_) => AccountStore(),
        child: MobView(),
      ),
    );
  }
}

class MobView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('mobView build');
    AccountStore store = Provider.of<AccountStore>(context);
    print('not in action ${store.count}');
    return Container(
      child: Column(
        children: <Widget>[
          Observer(
            builder: (_) {
              print('count ${store.count}');
              return Text('${store.count}');
            },
          ),
          Observer(
            builder: (_) {
              print('state ${store.state}');
              return Text('${store.state}');
            },
          ),
          Row(
            children: <Widget>[
              RaisedButton.icon(
                onPressed: () {
                  store.increment();
//                  store.count++;
                },
                icon: Icon(Icons.add),
                label: Text('label'),
              ),
              RaisedButton.icon(
                onPressed: () {
                  store.loadUser();
                },
                icon: Icon(Icons.cloud_download),
                label: Text('label'),
              ),
              RaisedButton(
                child: Text('添加list'),
                onPressed: () {
                  store.addList(Random().nextInt(100));
                },
              ),
            ],
          ),
          Observer(
            builder: (_) => ListView(
              shrinkWrap: true,
              children: store.list != null
                  ? store.list?.map((i) => Text(i.toString()))?.toList()
                  : <Widget>[],
            ),
          ),
        ],
      ),
    );
  }
}
